package com.zenghm.recyclerview.paging;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zenghm.recyclerview.paging.databinding.ItemDragBinding;

import java.util.List;

public class DragRecyclerViewAdapter extends RecyclerView.Adapter<DragRecyclerViewAdapter.VH> {

    private final Context mContext;
    private final List<String> mList;

    public DragRecyclerViewAdapter(Context context, List<String> list) {
        mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public DragRecyclerViewAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ItemDragBinding binding = ItemDragBinding.inflate(mInflater, parent, false);
        return new VH(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull final DragRecyclerViewAdapter.VH holder, int position) {
        holder.setName(mList.get(position));
        holder.itemView.setOnClickListener(v -> {
            String clickInfo = mList.get(holder.getAdapterPosition());
            Toast.makeText(mContext, clickInfo, Toast.LENGTH_LONG).show();
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class VH extends RecyclerView.ViewHolder {

        private final ItemDragBinding mBinding;

        private VH(ItemDragBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void setName(CharSequence text) {
            mBinding.tvName.setText(text);
        }
    }
}